﻿using DevExpress.XtraEditors;
using MesProjectWinform.Production.Models;
using MesProjectWinform.Public;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MesProjectWinform.Production.Forms.ProductLock
{
    public partial class ProductLockForm : DevExpress.XtraEditors.XtraForm
    {
        public ProductLockForm()
        {
            InitializeComponent();
        }

        private void ProductLockForm_Load(object sender, EventArgs e)
        {
            #region 初始化工单类型下拉框
            //为lookUpEdit1绑定数据源并且设置分开设置显示的字段和实际存储的字段
            lookUpEdit1.Properties.DataSource = new List<TypeModel>()
            {
                //Id看情况加，如果Id是用来作为Value的话就加
                new TypeModel(){Name="产品"},
                new TypeModel(){Name="主数据"},
                new TypeModel(){Name="工单"}
            };
            lookUpEdit1.Properties.DisplayMember = "Name"; // 显示的字段
            lookUpEdit1.Properties.ValueMember = "Name"; // 实际存储的字段
            //在显示时只显示Name字段
            lookUpEdit1.Properties.PopulateColumns();
            lookUpEdit1.Properties.Columns[0].Visible = false; // 隐藏Id字段
            //下拉框启动时默认显示请选择
            lookUpEdit1.Properties.NullText = "请选择类型";
            #endregion

            #region 初始化工序下拉框
            lookUpEdit2.Properties.DataSource = new List<TypeModel>()
            {
                new TypeModel(){Id=1,Name="工序1"},
                new TypeModel(){Id=2,Name="工序2"}
            };
            lookUpEdit2.Properties.DisplayMember = "Name"; // 显示的字段
            lookUpEdit2.Properties.ValueMember = "Id"; // 实际存储的字段

            //在显示时只显示Name字段
            lookUpEdit2.Properties.PopulateColumns();
            lookUpEdit2.Properties.Columns[0].Visible = false; // 隐藏Id字段

            //下拉框启动时默认显示请选择
            lookUpEdit2.Properties.NullText = "请选择工序";
            #endregion
        }

        private void labelControl5_Click(object sender, EventArgs e)
        {

        }

        private async void simpleButton1_Click(object sender, EventArgs e)
        {
            object productLockType = lookUpEdit1.EditValue;
            object code = memoEdit1.Text.Trim();
            using (var client = new HttpClient())
            {
                var respnse = await client.GetStringAsync($"http://localhost:5238/api/ProductLock/GetProductLocks?Type={productLockType}&Code={code}");
                var result = JsonConvert.DeserializeObject<ApiResult<List<ProductLockOutput>>>(respnse);
                gridControl1.DataSource = result.Data;
                var gridView = gridControl1.MainView as DevExpress.XtraGrid.Views.Grid.GridView;
                if (result.Code == ApiEnums.Success)
                {
                    gridView.Columns.Clear();
                    // 自定义列：只显示你想要的字段，并自定义表头
                    gridView.Columns.AddVisible("Type", "类型"); // 添加类型列
                    gridView.Columns.AddVisible("Code", "编码"); // 添加编码列
                    gridView.Columns.AddVisible("工序"); // 添加编码列
                    gridView.Columns.AddVisible("锁定起止日期"); // 添加编码列
                    gridView.Columns.AddVisible("操作"); // 添加编码列
                }
                else
                {
                    MessageBox.Show(result.Message);
                }
            }

        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            LockReasonForm form = new LockReasonForm();
            ProductLockInput model = new ProductLockInput()
            {
                PRODUCTLOCKBEGINTIME = (DateTime)dateEdit1.EditValue,
                PRODUCTLOCKENDTIME = (DateTime)dateEdit2.EditValue,
                PRODUCTLOCKPRODUCTION = lookUpEdit2.EditValue.ToString(),
                PRODUCTLOCKTYPE = gridView1.GetRowCellValue(0, "Type"), // 获取选中的类型
                PRODUCTLOCKCODE = memoEdit1.Text // 获取选中的编码
            };
            form.GetData(model);
            form.ShowDialog(); // 显示锁定原因输入框
        }
    }
}